package jun.learn.jackson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;

public class TestJackson {
	
	private static final ObjectMapper om = new ObjectMapper();
	public static void main(String[] args) {
		List<User> users = new ArrayList<User>();
		User user = new User();
		user.setAge(10);
		user.setBirthday(new Date());
		user.setNo("123123");
		user.setPassword("123123");
		user.setUsername("xujun");
		
		User user1 = new User();
		user1.setAge(10);
		user1.setBirthday(new Date());
		user1.setNo("123123");
		user1.setPassword("123123");
		user1.setUsername("xujun");
		
		User user2 = new User();
		user2.setAge(10);
		user2.setBirthday(new Date());
		user2.setNo("123123");
		user2.setPassword("123123");
		user2.setUsername("xujun");
		users.add(user);
		users.add(user1);
		users.add(user2);
		try {
			om.getSerializationConfig().setSerializationInclusion(Inclusion.ALWAYS);
			String jsonStr = om.writeValueAsString(users);
			System.out.println(jsonStr);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
